import {ORDER_LIST,REFRESH_LIST,LOGISTICS_LIST} from './constants';

import NetUtil from '../../common/NetUtil';
import config from '../../config/config';

import {requestStart,requestEnd} from '../../commonActions/requestAction'

import {showLoading,hideLoading,toast} from '../../utils/NativeUtil';

export const getOrderList =  (token, orderState, page,start,success,error) => {

    return dispatch => {

        let os = orderState;
        
        if (os == 7){
            os = '7;8';
        }
        const params = {
            token: token,
            state: os,
            page: page,
            size: 10
        };


        let url = "/s/order/list_mul_state";

        if (orderState == "10") {
            url = "/s/order/listByQr";
        }
        
        start();

        NetUtil.postSign(config.apiUrl + url, params, function(res) {
            if (res.success) {
                dispatch({
                    type:ORDER_LIST,
                    payload:{
                        orderState:orderState,
                        orderList:res.data
                    }
                })
                success();
            } else {
                error();
            }
            })
        }

};

export const getLogisticsInfo = (id) => {
    return dispatch => {
        let url = "/s/express/info";

        const params = {
            orderId: id
        };

        NetUtil.postSign(config.apiUrl + url, params, function(res) {
            if (res.success) {
                let data = res.data;
                let name = data.company.name;
                let tel = data.company.tel;
                let number = data.number;
                let detail = data.detail;
                let list = JSON.parse(detail);
                dispatch({
                    type: LOGISTICS_LIST,
                    payload: {
                        logisticsName: name,
                        logisticsTel: tel,
                        logisticsNumber: number,
                        logisticsList: list,
                    }
                })
            } else {
                dispatch({
                    type: LOGISTICS_LIST,
                    payload: {
                        logisticsName: '',
                        logisticsTel: '',
                        logisticsNumber: '',
                        logisticsList: [],
                    }
                })
            }
        })
    }
};

export const refreshOrderList =  (token, orderState, page,success,error) => {

    return dispatch => {

        let os = orderState;
        
        if (os == 7){
            os = '7;8';
        }
        const params = {
            token: token,
            state: os,
            page: page,
            size: 10
        };

        let url = "/s/order/list_mul_state";

        if (orderState == "10") {
            url = "/s/order/listByQr";
        }
        dispatch(requestStart());

    NetUtil.postSign(config.apiUrl + url, params, function(res) {

        dispatch(requestEnd());
        if (res.success) {
                dispatch({
                    type:REFRESH_LIST,
                    payload:{
                        orderState:orderState,
                        orderList:res.data
                    }
                })
            success();
        } else {
            error();
        }
        })
    }

};

export const orderCancle = (params,success,error) => {
    return dispatch => {
        showLoading("正在取消");
        // NetUtil.postSign(config.apiUrl + '/s/order/cancle', params, function(res) {
        NetUtil.postSign('https://mapi.hongheyu1688.com/mapi/s/order/cancle', params, function(res) {
            hideLoading();
            if (res.success) {
                toast("订单已取消");
                success();
            } else {
                error();
                toast(res.message)
            }
      })
    }
}

export const orderToSend = (params,success,error) => {
    return dispatch => {
        showLoading("正在提交");
        NetUtil.postSign(config.apiUrl + '/s/order/send', params, function(res) {
            hideLoading();
            if (res.success) {
                toast("提交成功");
                success();
            } else {
                error();
                toast(res.message)
            }
      })
    }
}


